package book.action;

import static common.utils.Constants.LIMIT;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.impl.OrderDetailServiceImpl;
import service.impl.OrderServiceImpl;

import common.action.BaseAction;
import common.pojo.Order;
import common.pojo.OrderDetail;
import common.utils.Constants;
import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;
import common.utils.SessionUtils;

@Scope("prototype")
@Controller(value = "order")
public class OrderAction extends BaseAction {

	
	/**
	 * 获取列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getOrderListByPage() throws Exception {
		
		String currentTmp = request.getParameter("current");//获取页码参数
		Order o = new Order();
		o.setDeleted(Constants.NOT_DELETED);
		String state = request.getParameter("state");
		if (StringUtils.isNotEmpty(state)){
			o.setState(Integer.parseInt(state));
		}
		int total= orderService.queryByCount(o);//查询符合条件的类型数量
		int current = PageUtil.getCurrent(currentTmp);//判断当前页码数
		Page page = PageUtil.createPage(LIMIT, total, current);//生成页码实体参数
		orderlist = orderService.find(page.getBeginIndex(), LIMIT,state);//根据页码参数以及每页显示数量获取列表
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getOrderListByPage.action?state="+state,"");//页码显示的静态代码，字符串，很长
		return "orderList";
	}

	/**
	 * 获取查询列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		
		String currentTmp = request.getParameter("current");//获取页码参数
		Order o = new Order();
		o.setDeleted(Constants.NOT_DELETED);
		String state = request.getParameter("state");
		String orderdate = request.getParameter("orderdate");
		String key = request.getParameter("key");
		int total= orderService.queryByCount(state,orderdate,key);//查询符合条件的类型数量
		int current = PageUtil.getCurrent(currentTmp);//判断当前页码数
		Page page = PageUtil.createPage(LIMIT, total, current);//生成页码实体参数
		orderlist = orderService.search(page.getBeginIndex(), LIMIT,state,orderdate,key);//根据页码参数以及每页显示数量获取列表
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getOrderListByPage.action?state="+state+"&key="+key+"&orderdate="+orderdate,"");//页码显示的静态代码，字符串，很长
		return "orderList";
	}
	
	/**
	 * 更新订单状态
	 * @return
	 */
	public String updateState() {
		
		Integer state= Integer.parseInt(request.getParameter("state"));
		Integer id= Integer.parseInt(request.getParameter("id"));
		Order order=new Order();
		order.setId(id);
		if (state==2){
			order.setPayTime(new Date());
			
		}
		else if (state==3){
			order.setSendTime(new Date());
			
		}
		else if (state==4){
			order.setAcceptTime(new Date());
		}
		order.setState(state);
		order.setUpdateName(SessionUtils.getAdminName(request));
		order.setUpdateTime(new Date());
		
		orderService.updateBySelective(order);
		if(StringUtils.isBlank(dofunction))dofunction="()";//刷新页面
		return SUCCESS;
	}


	/**
	 * 按id查询
	 * @return
	 */
	public String findInfoById() {
		String id = request.getParameter("id");
		order=orderService.findInfoById(id);
		orderDlist=orderDService.find(id);
		return "info";
	}
	private static final long serialVersionUID = 1L;

	//引用service层
	@Resource
	private OrderServiceImpl orderService;
	@Resource
	private OrderDetailServiceImpl orderDService;
	private Order order;
	private List<Order> orderlist;//列表
	private List<OrderDetail> orderDlist;//列表
	private String pagination;//分页
	private String dofunction;//返回处理刷新



	public List<OrderDetail> getOrderDlist() {
		return orderDlist;
	}

	public void setOrderDlist(List<OrderDetail> orderDlist) {
		this.orderDlist = orderDlist;
	}

	public Order getOrder() {
		return order;
	}


	public void setOrder(Order order) {
		this.order = order;
	}

	public List<Order> getOrderlist() {
		return orderlist;
	}


	public void setOrderlist(List<Order> orderlist) {
		this.orderlist = orderlist;
	}


	public String getDofunction() {
		return dofunction;
	}

	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}



}
